package com.yc.mvc.web;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/5/3
 * Email haijunzhou@hnit.edu.cn
 */
@RestController  // 默认每个方法的返回值， 转换为 json 字符串
@RequestMapping("page") // 该类所有的方法的地址前缀
public class IndexController {

    @RequestMapping({"hello","hello1"})  // /page/hello
    String hello(){
        return "hello RestController";
    }

    @RequestMapping(path = "toIndex")
    ModelAndView toIndexJsp(){
        ModelAndView mav = new ModelAndView();
        // 使用绝对路径
        mav.setViewName("redirect:/SpringMVC_war_exploded/index.jsp");
        return mav;
    }

//    @RequestMapping(value="method1",
//    method = RequestMethod.GET)
    @GetMapping("method1")
    String getMethod(){
        return "getMethod";
    }
//    @RequestMapping(value="method2",
//            method = RequestMethod.POST)
    @PostMapping("method2")
    String postMethod(){
        return "postMethod";
    }

    @RequestMapping(value="method3",
            method = {RequestMethod.POST, RequestMethod.GET})
    String postAndGetMethod(){
        return "postAndGetMethod";
    }

    @RequestMapping(path="add", params = "a=100")
    int add(int a, int b){
        return a+b;
    }

    @GetMapping(value="host", headers = "Host=localhost:8080")
    String header(){
        return "host";
    }

    // 对请求体的内容格式限定
    // APPLICATION_FORM_URLENCODED_VALUE 表格格式
    @PostMapping(value = "consurme",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    String consurme(){
        return "consurme";
    }

    @GetMapping(value="pro1", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
    String pro1(){
        return "<h1>响应体内容格式</h1>";
    }

    @GetMapping(value="pro2", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    String pro2(){
        return "<h1>响应体内容格式</h1>";
    }

}
